﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtAspNet;

namespace HY.WEB.HR
{
    public partial class mem_add : PageBase
    {
        public string us, whe = "", path = "";           
        protected void Page_Load(object sender, EventArgs e)
        {
            us = Session["ID"].ToString();
            if (!IsPostBack)
            {
                //if (!String.IsNullOrEmpty(Request["stype"]))
                //{
                //     hidstype.Text = Request["stype"].Trim();
                
                //}
                fill(); 
            }

        
        }

        //填 充ddl
        void fill()
        {
            BLL.HR.hr_ot u = new BLL.HR.hr_ot();
            DataTable e;


            //填充部门
            e = u.getlist_dept(whe);
            ddldetp.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddldetp.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddldetp.DataBind();
            ddldetp.Items.Add(new ExtAspNet.ListItem("", ""));
            ddldetp.SelectedIndex = ddldetp.Items.IndexOf(ddldetp.Items.FindByText("业务部"));

            //填充职位
            e = u.getlist_job(whe);
            ddljob.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddljob.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddljob.DataBind();
            ddljob.Items.Add(new ExtAspNet.ListItem("", ""));
            ddljob.SelectedIndex = ddljob.Items.IndexOf(ddljob.Items.FindByText("职员"));

            //填充角色
            e = u.getlist_role(whe);
            ddlrole.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddlrole.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddlrole.DataBind();
            ddlrole.Items.Add(new ExtAspNet.ListItem("", ""));
            ddlrole.SelectedIndex = ddlrole.Items.IndexOf(ddlrole.Items.FindByText("员工角色"));

            //填充学历
            e = u.getlist_edu(whe);
            ddledu.Items.Clear();
            for (int i = 0; i < e.Rows.Count; i++)
            {
                ddledu.Items.Add(new ExtAspNet.ListItem(e.Rows[i][2].ToString(), e.Rows[i][2].ToString()));
            }
            ddledu.DataBind();
            ddledu.Items.Add(new ExtAspNet.ListItem("", ""));
            ddledu.SelectedIndex = ddledu.Items.IndexOf(ddledu.Items.FindByText("高中"));
        }
      
  
        protected void btnSubmit_Click(object sender, EventArgs e)
        {  
            //文件上传
          
            string fileName = fileup.ShortFileName;
             if (fileName.Length >0)
                    {
                        fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                        fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
                        path = "~/file/hr/" + fileName;
                        if (fileup.HasFile)
                            fileup.SaveAs(Server.MapPath(path));

                    }


             Model.HR.sys_user s = new Model.HR.sys_user();

             s.sbh = BLL.BllAutoCode.GetMaxBH("sys_user");

             if (txtlgid.Text == null || txtlgid.Text == "")
             {
                 Alert.Show("账号不能为空!");
                 return;
             }

             if (BLL.Bllbase.check_exits("sys_user", "lgid='" + txtlgid.Text + "'"))
             {
                 Alert.Show("账号已存在，请输入其它账号名！");
                 return;
             }

             s.lgid = txtlgid.Text;

             s.lgpw = BLL.sys.chkpw.Md5(txtlgpw.Text);

             s.user_name = txtnm.Text;
             s.sex = ddlsex.Text;

             if (VAN.IsDateTime(indate.Text))
                 s.indate = DateTime.Parse(indate.Text);
             else
                 s.indate = DateTime.Parse("1900-01-01");

             if (VAN.IsDateTime(birthday.Text))
                 s.birthday = DateTime.Parse(birthday.Text);
             else
                 s.birthday = DateTime.Parse("1900-01-01");

             if (VAN.IsDateTime(outdate.Text))
                 s.outdate = DateTime.Parse(outdate.Text);
             else
                 s.outdate = DateTime.Parse("1900-01-01");

             s.school = "";
             s.school = txtschool.Text;

             s.edu = "";
             s.edu = ddledu.SelectedValue;

             s.major = "";
             s.major = major.Text;

             s.dept = "";
             s.dept = ddldetp.SelectedValue;

             s.role = "";
             s.role = ddlrole.SelectedValue;

             s.job = "";
             s.job = ddljob.SelectedValue;

             s.jg = "";
             s.jg = txtjg.Text;

             s.hj_address = "";
             s.hj_address = txtaddress.Text;

             s.cur_address = "";
             s.cur_address = txtnewadd.Text;

             s.code = "";
             s.code = txtcode.Text;

             s.tel = "";
             s.tel = txttel.Text;

             s.r_name = "";
             s.r_name = txtjustperson.Text;

             s.r_tel = "";
             s.r_tel = txtjusttel.Text;

             s.QQ = "";
             s.QQ = txtqq.Text;

             s.email = "";
             s.email = txtemail.Text;

             s.marry = "";
             s.marry = ddlmarry.SelectedValue; //婚否
             s.pic = path;
             s.status = 0; //开通
             s.status = int.Parse(ddlstatus.SelectedValue);
             s.inout = int.Parse(ddlinout.SelectedValue);  //在职

             try
             {
                 BLL.HR.sys_user t = new BLL.HR.sys_user();
                 t.Add(s);

                 //添加权限
                 DAL.DbHelperSQL.ExecuteNonQuery("insert into user_safe(ubh,uname,mname,menu)" +
                  " values('" + s.sbh + "','" + s.user_name + "','业务人员','sales.xml')");
             }
             catch
             {

                 Alert.Show("新增错误，请详细检查输入格式，是否正确！");
                 return;

             }


           //string title = txttitel.Text;
            //string person =us;
           // string day = txtday.Text;
          //  string memo = HttpUtility.HtmlEncode(FCKeditor1.Value);
          //  string memo = HttpUtility.HtmlEncode(FCKeditor1.Value);
        
         

           // BLL.oa.ad.insert(title, person, day, memo, path, hidstype.Text);


            Alert.Show("添加完成 ");
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(hidstype.Text) + ActiveWindow.GetHidePostBackReference());
        }

        
       
    }
}